/**
 * Classe para manipular objetos do google maps.
 */

var mapa;
var gdir;

function setDirections(fromAddress, toAddress) 
{
	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": "pt"});
}

function Gmapso()
{
	this.lat       = "-25.427778";
	this.lng       = "-49.273056";
	this.div       = "mapso";
	this.zoom      = 13;
	this.draggable = true;
	this.click     = null;
	this.ponto     = null;
	
	var center;
	//var marker;
	var ponto;
	var infoHtmlMove;
	
	this.setInfoHtmlMove = function(io)
	{
		infoHtmlMove = io;
	}
	
	/**
	 * Cria mapa.
	 */
	
	this.setMapa = function()
	{
		mapa   = new GMap2(document.getElementById(this.div));
		
        mapa.addControl(new GLargeMapControl());
        mapa.addControl(new GMapTypeControl());
        mapa.addControl(new google.maps.LocalSearch(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,20)));
        mapa.enableScrollWheelZoom(); 
        
        this.center();
	}
	
	/**
	 * Coloca o mapa no centro da lat e lng.
	 */
	 
	 this.center = function()
	 {
	 	center = new GLatLng(this.lat, this.lng);
	 	
	 	mapa.setCenter(center, this.zoom);
	 }
	
	/**
	 * Cria marcador.
	 */
	
	this.createMarker = function(infoHtml)
	{
		var marker = new GMarker(new GLatLng(this.lat, this.lng), {draggable: this.draggable});
		
		/**
		 * Drag e Drop.
		 */
		 
		if(this.draggable)
		{
			GEvent.addListener(marker, "dragstart", function(){
				mapa.closeInfoWindow();
			});
		
			GEvent.addListener(marker, "dragend", function(){
				ponto = marker.getLatLng().toUrlValue(4);
				
				$("#pontoP").val(ponto);
				
				if(infoHtmlMove)
				{
					marker.openInfoWindowHtml(infoHtmlMove);
				}
				
				//this.center();
			});			
		}
		
		if(this.click)
		{
			GEvent.addListener(marker, "click", function()
			{
				marker.openInfoWindowHtml(infoHtml);
			});
		}
		
		/**
		 * Adicionar marcador ao mapa.
		 */
		 
		mapa.addOverlay(marker);		

		/**
		 * Abre mensagem na inicializacao do marcador.
		 */
		 
		if(infoHtml && this.draggable)
		{
			marker.openInfoWindowHtml(infoHtml);
		}
	}
	
	this.direction = function()
	{
		gdir = new GDirections(mapa, document.getElementById("directions"));
		gdir.load();
	}
}